/*
 * MapViewer.java
 *
 * Created on Jun 14, 2007, 8:11:57 PM
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.tools;

import com.jme.app.SimpleGame;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.SharedMesh;
import com.jme.scene.Skybox;
import com.jme.scene.shape.Box;
import com.jme.scene.state.MaterialState;
import java.io.File;
import javax.swing.JFileChooser;
import kanjitori.abstraction.AbstractFactoryXmlLoader;
import kanjitori.map.Content;
import kanjitori.map.ExtensionMapLoader;
import kanjitori.map.Map;
import kanjitori.map.Position;
import kanjitori.util.GraphicUtil;

/**
 *
 * @author DGronau
 */
public class MapViewer extends SimpleGame {
    
    private Map map;
    private Node contentNode = new Node();
    private Skybox skybox;
    private long last = System.currentTimeMillis();
    
    public MapViewer(Map map) {
        this.map = map;
    }
    
    public static void main(String... args) {
        AbstractFactoryXmlLoader.init("factories.xml");
        JFileChooser jfc = new JFileChooser("./map");
        int retVal = jfc.showOpenDialog(null);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            File file = jfc.getSelectedFile();
            Map map = ExtensionMapLoader.getInstance().get(file.getAbsolutePath());
            MapViewer app = new MapViewer(map);
            app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
            app.start();
        }
        
    }
    
    protected void simpleInitGame() {
        rootNode.attachChild(map.getNode());
        Box box = new Box("box", new Vector3f(), 0.02f, 1.5f, 0.02f);
        MaterialState ms = display.getRenderer().createMaterialState();
        ms.setEmissive(ColorRGBA.white);
        box.setRenderState(ms);
        for (int x = 0; x < map.getSize().x(); x++) {
            for (int y = 0; y < map.getSize().y(); y++) {
                if (map.getContentMatrix().getContent(Position.get(x, y)) == Content.EMPTY) {
                    SharedMesh sn = new SharedMesh("box", box);
                    sn.setLocalTranslation(new Vector3f(2 * x, 0 , 2 * y));
                    contentNode.attachChild(sn);
                }
            }
        }
        skybox = GraphicUtil.getSkybox(map.getSkyboxTextures());

        KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
        manager.set("sky", KeyInput.KEY_X);
        manager.set("content", KeyInput.KEY_C);
    }
    
    public void simpleUpdate() {
        if (System.currentTimeMillis() - last < 200) {
            return;
        }
              
        KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
        
        if (manager.isValidCommand("sky")) {
            if (skybox.getParent() == null) {
                rootNode.attachChild(skybox);
            } else {
                rootNode.detachChild(skybox);
            }
            rootNode.updateRenderState();
            last = System.currentTimeMillis();
        }

        if (manager.isValidCommand("content")) {
            if (contentNode.getParent() == null) {
                rootNode.attachChild(contentNode);
            } else {
                rootNode.detachChild(contentNode);
            }
            rootNode.updateRenderState();
            last = System.currentTimeMillis();
        }


    }
    
}
